package com.dsteiner.widgets.client;

import java.util.ArrayList;
import java.util.List;

import com.dsteiner.widgets.client.Widgets.FlowPanelExt;
import com.dsteiner.widgets.client.Widgets.FocusPanelExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.IWidgetBuilder;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.controller.SuggestBoxController;
import com.dsteiner.widgets.client.controller.SuggestionController;
import com.dsteiner.widgets.client.domain.BaseObject;
import com.dsteiner.widgets.client.model.SuggestBoxModel;
import com.dsteiner.widgets.client.model.SuggestionModel;
import com.dsteiner.widgets.client.resources.WidgetsResources;
import com.google.gwt.http.client.Request;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class SuggestBoxWidget extends LazyWidget implements IWidgetBuilder{

	public static class SuggestionRequest extends BaseObject{
		private String text;
		private String className;
		private int displayCount = 10;
		
		public SuggestionRequest() {
		}
		public SuggestionRequest(String text) {
			this.text = text;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public void setClassName(String className) {
			this.className = className;
		}
		public String getClassName() {
			return className;
		}
		public int getDisplayCount() {
			return displayCount;
		}
		public void setDisplayCount(int displayCount) {
			this.displayCount = displayCount;
		}
	}
	
	public static class Suggestion extends BaseObject{
		private String displayText;
		public Suggestion() {
		}
		public Suggestion(String displayText) {
			this.displayText = displayText;
		}
		public void setDisplayText(String displayText) {
			this.displayText = displayText;
		}
		public String getDisplayText() {
			return displayText;
		}
		public boolean equals(Object obj) {
			if (obj == null)
				return false;
			if (!(obj instanceof Suggestion))
				return false;
			Suggestion other = (Suggestion) obj;
			return ObjectUtil.equals(other.displayText, displayText);
		}
	}
	
	public static class SuggestionResult extends BaseObject{
		private List<Suggestion> suggestions = new ArrayList<Suggestion>();
		
		public SuggestionResult() {
		}
		
		public void setSuggestions(List<Suggestion> suggestions) {
			this.suggestions = suggestions;
		}
		public List<Suggestion> getSuggestions() {
			return suggestions;
		}
	}
	
	public interface SuggestBoxDataProvider{
		public Request requestSuggestions(SuggestionRequest request, AsyncCallback<SuggestionResult> callback);
	}

	public class SuggestionWidget extends LazyWidget {

		private SuggestionController suggestionController = new SuggestionController();
		private final SuggestionModel suggestionModel;

		public SuggestionWidget(SuggestionModel suggestionModel) {
			this.suggestionModel = suggestionModel;
			setStyleName(WidgetsResources.INSTANCE.css().suggestionWidget());
		}

		@Override
		public Widget createContentWidget() {
			FocusPanelExt mainPanel = new FocusPanelExt();
			HTMLExt html = new HTMLExt();

			mainPanel.setWidget(html);
			
			suggestionController.registerFocusPanel(mainPanel);
			suggestionController.registerHtml(html);
			suggestionController.registerSuggestBoxModel(suggestBoxModel);
			suggestionController.registerSuggestionWidget(this);
			suggestionController.registerSuggestionModel(suggestionModel);
			return mainPanel;
		}

	}
	
	private SuggestBoxController suggestBoxController = new SuggestBoxController();
	private final SuggestBoxModel suggestBoxModel;
	private final SuggestBoxDataProvider suggestBoxDataProvider;
	private PrefilledTextInput textInput;

	public SuggestBoxWidget(SuggestBoxModel suggestBoxModel, SuggestBoxDataProvider suggestBoxDataProvider) {
		this.suggestBoxModel = suggestBoxModel;
		this.suggestBoxDataProvider = suggestBoxDataProvider;
		setStyleName(WidgetsResources.INSTANCE.css().suggestBoxWidget());
		textInput = new PrefilledTextBox(suggestBoxModel);
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		FlowPanelExt popup = new FlowPanelExt();
		HTMLExt loadingHtml = new HTMLExt("Cargando sugerencias...");
		HTMLExt noResultsHtml = new HTMLExt("no hay resultados");
		
		mainPanel.add(textInput);
		mainPanel.add(popup);
		
		popup.addStyleName(WidgetsResources.INSTANCE.css().popup());
		textInput.setWidth("100%");
		
		suggestBoxController.registerLoadingHtml(loadingHtml);
		suggestBoxController.registerNoResultsHtml(noResultsHtml);
		suggestBoxController.registerSuggestBoxDataProvider(suggestBoxDataProvider);
		suggestBoxController.registerPopup(popup);
		suggestBoxController.registerSuggestBoxWidget(this);
		suggestBoxController.registerSuggestBoxModel(suggestBoxModel);
		suggestBoxController.registerSuggestionWidgetBuilder(this);
		
		return mainPanel;
	}

	public IWidget createWidget(Object model) {
		SuggestionModel suggestion = (SuggestionModel) model;
		SuggestionWidget suggestionWidget = new SuggestionWidget(suggestion);
		return suggestionWidget;
	}

	public void setTabIndex(int index) {
		textInput.setTabIndex(index);
	}

}